//
//  MTPlayGroundFoucsVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/15.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTPlayGroundFoucsVC.h"
#import "MTPlayGroundCell.h"

#import "MTQuestionAnswerVC.h"

@interface MTPlayGroundFoucsVC ()
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation MTPlayGroundFoucsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataArr = [NSMutableArray new];
    
    self.tableview =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-TabbarHeight) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadData];
    }];
    
    [self getInfo];
    
//    [self createViewBynoMsgName:@"暂无动态" alertMsg:@"达萨罗的男的"];
//    [self.tableview addSubview:self.noMsgView];
//    self.noMsgView.hidden = NO;
}
-(void)loadData{
    if (self.tableview.mj_header ) {
        [self.tableview.mj_header endRefreshing];
    }
    if (self.tableview.mj_footer ) {
        [self.tableview.mj_footer endRefreshing];
    }
    [self getInfo];
}
- (void)getInfo {
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
                self.dataArr = [NSMutableArray arrayWithArray:data];
            }else {
                NSArray *info = data;
                if (info.count == 0) {
                    self.page --;
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }else if (info.count < 10) {
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }
                for (MTPlayGroundModel *model in info) {
                    [self.dataArr  addObject:model];
                }
            }
            
            for (MTPlayGroundModel *model in self.dataArr) {
                Message *msg =  [[Message alloc] init];
                msg.userId =[NSString stringWithFormat:@"met_%@",model.usersId];
                msg.username = [NSString stringWithFormat:@"%@", model.nickName];
                msg.headImageUrl = [NSString stringWithFormat:@"%@",model.portraitMap];
                [Message insertObject:msg];
            }
           
            
            [self.tableview reloadData];
            //            if (self.dataArr.count == 0) {
//                            [self createViewBynoMsgName:@"暂无动态" alertMsg:@""];
//                            [self.view addSubview:self.noMsgView];
//                            self.noMsgView.hidden = NO;
            //            }else {
            //                self.noMsgView.hidden = YES;
            //            }
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc storyFindByFriends:[NSString stringWithFormat:@"%ld",(long)self.page]];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTPlayGroundCell *cell = [MTPlayGroundCell cellWithTableView:tableView];
    cell.model  = [self.dataArr objectAtIndex:indexPath.section];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 176;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MTPlayGroundModel *model = [self.dataArr objectAtIndex:indexPath.section];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            MTQuestionAnswerVC *vc = [[MTQuestionAnswerVC alloc] init];
            vc.dataArr = [data objectForKey:@"questionList"];
            vc.leftName = model.nickName;
            vc.rightName = KUSER.nickName;
            vc.storyId = model.storyId;
            vc.storyVersion = model.storyVersion;
            vc.createUsersId = model.usersId;
            
            vc.hasAnswer = ^(NSString *  num) {
                [self.dataArr removeObjectAtIndex:indexPath.section];
                [self.tableview reloadData];
            };
            
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findStoryByStoryId:model.storyId];
}

@end
